//
//  LoginViewController.m
//  jizhiyunduan
//
//  Created by 鞠超 on 16/4/28.
//  Copyright © 2016年 JC. All rights reserved.
//

#import "LoginViewController.h"

@interface LoginViewController () <UITextFieldDelegate , UIScrollViewDelegate>
//账号
@property (nonatomic, strong) UITextField * text_account;
//密码
@property (nonatomic, strong) UITextField * text_password;

//scrollView
@property (nonatomic, strong) UIScrollView * scrollView;


@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [SVProgressHUD setBackgroundColor:[UIColor whiteColor]];
    [SVProgressHUD setForegroundColor:[UIColor blackColor]];

    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void)p_navi
{
    _lblTitle.text = [NSString stringWithFormat:@"登录"];
    _lblTitle.font = [UIFont systemFontOfSize:20];
    
    [self addLeftButton:@"fanhui@2x.png"];

    //    _btnRight.hidden = YES;
    _lblRight.text = @"注册";
}

//显示tabbar
-(void)viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
}

//注册
- (void)clickRightButton:(UIButton *)sender
{
    
}

//左返回
- (void)clickLeftButton:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 布局
- (void)p_setupView
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    CGFloat length_x = (SCREEN_HEIGHT - 64) / 3;
    CGFloat length_picX = length_x / 3;
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    self.scrollView.backgroundColor = [UIColor whiteColor];
    self.scrollView.delegate = self;
    self.scrollView.contentSize = CGSizeMake(0, SCREEN_HEIGHT - 60);
    [self.view addSubview:self.scrollView];

    
    UIImageView * image_pic = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - length_picX * 0.75, length_picX * 0.75, length_picX * 1.5, length_picX * 1.5)];
    image_pic.backgroundColor = navi_bar_bg_color;
    image_pic.layer.cornerRadius = length_picX * 0.75;
    [self.scrollView addSubview:image_pic];
    
    
    UIView * view_account = [[UIView alloc] initWithFrame:CGRectMake(10, length_x, self.view.frame.size.width - 20, 50)];
    view_account.backgroundColor = [UIColor whiteColor];
    view_account.layer.cornerRadius = 5;
    view_account.layer.borderColor = navi_bar_bg_color.CGColor;
    view_account.layer.borderWidth = 1;
    [self.scrollView addSubview:view_account];
    
    UILabel * label_account = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, 60, 40)];
    label_account.text = @"账  号";
    label_account.font = [UIFont systemFontOfSize:17];
    label_account.textColor = navi_bar_bg_color;
    [view_account addSubview:label_account];
    
    self.text_account = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label_account.frame) + 10, 5, 200, 40)];
    //    self.text_account.backgroundColor = [UIColor orangeColor];
    self.text_account.placeholder = @"请输入账号";
    [view_account addSubview:self.text_account];
    self.text_account.delegate = self;
    
    
    UIView * view_password = [[UIView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(view_account.frame ) + 10, self.view.frame.size.width - 20, 50)];
    view_password.backgroundColor = [UIColor whiteColor];
    view_password.layer.cornerRadius = 5;
    view_password.layer.borderColor = navi_bar_bg_color.CGColor;
    view_password.layer.borderWidth = 1;
    [self.scrollView addSubview:view_password];
    
    UILabel * label_password = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, 60, 40)];
    label_password.text = @"密  码";
    label_password.textColor = navi_bar_bg_color;
    label_password.font = [UIFont systemFontOfSize:17];
    [view_password addSubview:label_password];
    
    self.text_password = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label_password.frame) + 10, 5, 200, 40)];
    self.text_password.placeholder = @"请输入密码";
    self.text_password.secureTextEntry = YES;
    [view_password addSubview:self.text_password];
    self.text_password.delegate = self;
    
    //登陆
    UIButton * btn_login = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_login.frame = CGRectMake(10, CGRectGetMaxY(view_password.frame) + 20, self.view.frame.size.width - 20 , 40);
    [btn_login setTitle:@"登 录" forState:(UIControlStateNormal)];
    [btn_login setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    btn_login.layer.cornerRadius = 4;
    btn_login.titleLabel.font = [UIFont systemFontOfSize:17];
    btn_login.backgroundColor = navi_bar_bg_color;
    [self.scrollView addSubview:btn_login];
    [btn_login addTarget:self action:@selector(btn_loginAction:) forControlEvents:(UIControlEventTouchUpInside)];

}

#pragma mark - 登陆按钮
- (void)btn_loginAction:(UIButton *)sender
{
//    NSLog(@"登陆");
    [self.text_account resignFirstResponder];
    [self.text_password resignFirstResponder];
    
    [UIView animateWithDuration:0.7 animations:^{
        
        self.scrollView.contentOffset = CGPointMake(0, 0);
        
    } completion:^(BOOL finished) {
        
    }];
    
    if([self.text_account.text length] == 0 || [self.text_password.text length] == 0)
    {
        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"账号或密码不能为空" preferredStyle:(UIAlertControllerStyleAlert)];
        [self presentViewController:alert animated:YES completion:^{
        }];
        
        UIAlertAction * action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alert addAction:action];
    }
    
    if([self.text_account.text length] != 0 && [self.text_password.text length] != 0)
    {
        DataProvider * dataprovider = [[DataProvider alloc] init];
        
        [dataprovider setDelegateObject:self setBackFunctionName:@"login:"];
        
        [dataprovider loginWithUsername:self.text_account.text password:self.text_password.text];
        
        [SVProgressHUD showWithStatus:@"正在登录"];
    }
}

#pragma mark - 接口部分
- (void)login:(id )dict
{
//    NSLog(@"%@",dict);
    
    if([dict[@"status"] integerValue] == 1)
    {
        @try
        {
            [SVProgressHUD dismiss];
        }
        @catch (NSException *exception)
        {
            
        }
        @finally
        {
            NSUserDefaults * userdefault = [NSUserDefaults standardUserDefaults];
            
            //保存用户名和密码
            [userdefault setObject:self.text_account.text forKey:@"account"];
            [userdefault setObject:self.text_password.text forKey:@"password"];
            //保存登陆的状态
            [userdefault setObject:@"1" forKey:@"Login_Success"];
            
            //保存用户信息（后期可能更多）
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"token"]] forKey:@"token"];
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"uid"]] forKey:@"uid"];
            //头像
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"avatar64"]] forKey:@"head_pic"];
            //站点数
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"servicenum"]] forKey:@"servicenum"];
            //用户名
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"username"]] forKey:@"username"];
            //余额
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"balance"]] forKey:@"balance"];
            //昵称
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"nickname"]] forKey:@"nickname"];

            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    else
    {
        [SVProgressHUD setMinimumDismissTimeInterval:1.0f];
        [SVProgressHUD showErrorWithStatus:@"登录失败"];
    }
}

#pragma mark - textField代理

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    [UIView animateWithDuration:0.7 animations:^{
        
        self.scrollView.contentOffset = CGPointMake(0, 0);
        
    } completion:^(BOOL finished) {
        
    }];
    
    return YES;
}
//touch
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.text_account resignFirstResponder];
    [self.text_password resignFirstResponder];
    
    [UIView animateWithDuration:0.7 animations:^{
        
        self.scrollView.contentOffset = CGPointMake(0, 0);
        
    } completion:^(BOOL finished) {
        
    }];
}

- (BOOL )textFieldShouldBeginEditing:(UITextField *)textField
{
    if(SCREEN_HEIGHT == 480)
    {
        [UIView animateWithDuration:0.7 animations:^{
            
            self.scrollView.contentOffset = CGPointMake(0, 130);
            
        } completion:^(BOOL finished) {
            
        }];
    }
    else if(SCREEN_HEIGHT == 568)
    {
        [UIView animateWithDuration:0.7 animations:^{
            
            self.scrollView.contentOffset = CGPointMake(0, 160);
            
        } completion:^(BOOL finished) {
            
        }];
    }
    else if(SCREEN_HEIGHT == 667)
    {
        [UIView animateWithDuration:0.7 animations:^{
            
            self.scrollView.contentOffset = CGPointMake(0, 190);
            
        } completion:^(BOOL finished) {
            
        }];
    }
    else
    {
        [UIView animateWithDuration:0.7 animations:^{
            
            self.scrollView.contentOffset = CGPointMake(0, 215);
            
        } completion:^(BOOL finished) {
            
        }];
    }
    
    return YES;
}

#pragma mark - scrollView代理
- (void )scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.text_account resignFirstResponder];
    [self.text_password resignFirstResponder];
    
    [UIView animateWithDuration:0.7 animations:^{
        
        self.scrollView.contentOffset = CGPointMake(0, 0);
        
    } completion:^(BOOL finished) {
        
    }];
}



@end
